package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.web.ServiceCode;
import cn.tedu.csmall.product.mapper.*;
import cn.tedu.csmall.product.pojo.entity.SPU;
import cn.tedu.csmall.product.pojo.entity.SPUDetail;
import cn.tedu.csmall.product.pojo.param.SPUAddNewParam;
import cn.tedu.csmall.product.pojo.vo.*;
import cn.tedu.csmall.product.service.SPUService;
import cn.tedu.csmall.product.util.IdUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class SPUServiceImpl implements SPUService {

    @Autowired
    SPUMapper spuMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    AlbumMapper albumMapper;
    @Autowired
    SPUDetailMapper spuDetailMapper;
    @Autowired
    IdUtils idUtils;

    @Override
    public void addNew(SPUAddNewParam spuAddNewParam) {

        // 检查品牌：是否存在，是否启用
        Long brandId = spuAddNewParam.getBrandId();
        BrandStandardVO brand = brandMapper.getStandardById(brandId);
        if (brand == null) {
            String message = "新增SPU失败，品牌数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        if (brand.getEnable() == 0) {
            String message = "新增SPU失败，品牌已经被禁用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 检查类别：是否存在，是否启用，是否不包含子级
        Long categoryId = spuAddNewParam.getCategoryId();
        CategoryStandardVO category = categoryMapper.getStandardById(categoryId);
        if (category == null) {
            String message = "新增SPU失败，类别数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        if (category.getEnable() == 0) {
            String message = "新增SPU失败，类别已经被禁用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        if (category.getIsParent() == 1) {
            String message = "新增SPU失败，选择的类别不允许包含子级类别！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 检查相册：是否存在
        Long albumId = spuAddNewParam.getAlbumId();
        AlbumStandardVO album = albumMapper.getStandardById(albumId);
        if (album == null) {
            String message = "新增SPU失败，相册数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        long spuId  = idUtils.generateSPUId();

        SPU spu = new SPU();
        BeanUtils.copyProperties(spuAddNewParam, spu);
        spu.setId(spuId);
        spu.setBrandName(brand.getName());
        spu.setCategoryName(category.getName());
        // 补全属性值：sales, comment_count, positive_comment_count >>> 0
        spu.setSales(0);
        spu.setCommentCount(0);
        spu.setPositiveCommentCount(0);
        // 补全属性值：is_deleted, is_published, is_new_arrival, is_recommend >>> 0
        spu.setIsDeleted(0);
        spu.setIsPublished(0);
        spu.setIsNewArrival(0);
        spu.setIsRecommend(0);
        // 补全属性值：is_checked >>> 0, check_user >>> null, gmt_check >>> null
        spu.setIsChecked(0);
        spu.setGmtCreate(LocalDateTime.now());
        spu.setGmtModified(LocalDateTime.now());
        int rows = spuMapper.insert(spu);
        if (rows != 1) {
            String message = "新增SPU失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }

        SPUDetail spuDetail = new SPUDetail();
        spuDetail.setSpuId(spuId);
        spuDetail.setDetail(spuAddNewParam.getDetail());
        spuDetail.setGmtCreate(LocalDateTime.now());
        spuDetail.setGmtModified(LocalDateTime.now());
        rows = spuDetailMapper.insert(spuDetail);
        if (rows != 1) {
            String message = "新增SPU失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public List<SPUListItemVO> list() {
        log.debug("开始处理【查询SPU列表】的业务");
        List<SPUListItemVO> spuList = spuMapper.list();
        if (spuList.size() == 0) {
            String message = "暂无SPU列表，请新增！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return spuList;
    }

    @Override
    public SPUFullInfoVO getFullInfoById(Long id) {
        log.debug("开始处理【根据ID查询一条SPU的全部信息】的业务");
        SPUFullInfoVO spuFullInfo = spuMapper.selectFullInfoById(id);
        if (spuFullInfo == null) {
            String message = "【根据ID查询一条SPU的全部信息】失败，SPU数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return spuFullInfo;
    }

    @Override
    public SPUStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询SPU信息】的业务");
        SPUStandardVO spuStandard = spuMapper.getStandardById(id);
        if (spuStandard == null) {
            String message = "【根据ID查询SPU信息】失败，SPU数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return spuStandard;
    }


}
